package com.dd.admin.business.sell.service.impl;


import com.dd.admin.business.memberCard.entity.MemberCard;
import com.dd.admin.business.memberCard.service.MemberCardService;
import com.dd.admin.business.sell.entity.Sell;
import com.dd.admin.business.sell.service.CancelSellService;
import com.dd.admin.business.sell.service.SellService;
import com.dd.admin.business.sellDetail.service.SellDetailService;
import com.dd.admin.common.exception.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

import static com.dd.admin.business.pay.enmus.CashierExceptionEnum.PAY_SELL_CARD_AMOUNT_CHANGE;


@Service
@Slf4j
public class CancelMemberBuildCard implements CancelSellService {
    @Autowired
    MemberCardService memberCardService;
    @Autowired
    SellDetailService sellDetailService;
    @Autowired
    SellService sellService;
    @Override
    public void cancel(Sell sell) {
        Sell extraSellInfo = sellService.getById(sell.getSellId());

        //消费时用的哪张会员卡
        MemberCard membercard = memberCardService.getById(extraSellInfo.getMemberCardId());
        BigDecimal sellCardAmount = extraSellInfo.getMemberCardAfterAmount();
        BigDecimal sellCardGiveAmount = extraSellInfo.getMemberCardAfterGiveAmount();
        log.info("订单显示的卡余额:"+sellCardAmount);
        BigDecimal cardAmount = membercard.getAmount();
        BigDecimal cardGiveAmount = membercard.getGiveAmount();
        log.info("当前卡余额:"+cardAmount);
        //当前余额和订单充值余额比较
        if(sellCardAmount.compareTo(cardAmount) != 0 || cardGiveAmount.compareTo(sellCardGiveAmount) != 0){
            throw new ApiException(PAY_SELL_CARD_AMOUNT_CHANGE);
        }
        //删除卡
        memberCardService.removeById(membercard);
    }
}
